// Retrofit Api Interface implement class,auto generation
package com.demo;

import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 *
 * @author GYL
 * @dateTime 20200507 22:46
 * @description  Net2:接口封装实现类
 * @see Net2
 *
 */
public class Net2Service {
  private static Net2 _innerCall;

  /**
   * 生成接口唯一码（WHAT）
   */
  private static final AtomicInteger sNextGeneratedId = new AtomicInteger(10);

  /**
   * 创建Api服务
   */
  private static Net2 innerCall() {
    if(_innerCall==null){
      _innerCall=com.okhttp.retrofit.RetrofitClient.getInstance().createService(Net2.class);
    }
    return _innerCall;
  }

  /**
   * 生成接口唯一码(What)
   */
  private static int generateWhat() {
    for (;;) {
      final int result = sNextGeneratedId.get();
       // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
        int newValue = result + 1;
        if (newValue > 0x00FFFFFF) {
        // Roll over to 1, not 0.
        newValue = 1;
    }
      if (sNextGeneratedId.compareAndSet(result, newValue)) {
      return result;  
    }
    }}

  /**
   *
   * {@link com.demo.Net2#getIndexPost} 
   * @param test 
   * @param handler  ResultBodyHandler<T> 
   *
   */
  public static <T> int getIndexPost(String test, ResultBodyHandler<T> handler) {
    int what=generateWhat()+handler.hashCode();
    handler.onBeforeExecute(what);
    Call<ResponseBody> call = innerCall().getIndexPost(test); 
    call.enqueue(handler); 
    return  what;
  }

  /**
   *
   * {@link com.demo.Net2#getIndexPost2} 
   * @param test 
   * @param handler  ResultHandler2<DataBean> 
   *
   */
  public static int getIndexPost2(String test, ResultHandler2<DataBean> handler) {
    int what=generateWhat()+handler.hashCode();
    handler.onBeforeExecute(what);
    Call<Result<DataBean>> call = innerCall().getIndexPost2(test); 
    call.enqueue(handler); 
    return  what;
  }
}
